package com.hhsjdesign.dynamicui.reddot

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.util.TypedValue
import android.widget.FrameLayout
import com.hhsjdesign.dynamicui.interfaces.IRedDot
import com.hhsjdesign.dynamicui.annotation.ViewLayout
import com.hhsjdesign.dynamicui.utils.RedDotUtil
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst

/**
 * 可加载背景的布局
 */
@ViewLayout(
    WidgetTypeConst.LAYOUT_FRAME
)
class RedDotFrameLayout : FrameLayout, IRedDot {
    var debugPaint: Paint? = null
    var debugText: String? = null

    constructor(context: Context) : this(context, null)
    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context, attrs: AttributeSet?, defStyleRes: Int) : super(
        context,
        attrs,
        0
    )

    private val redDotController: RedDotController

    init {
        setWillNotDraw(false)
        redDotController =
            RedDotController(30, 30, 5, 0xffff3333.toInt())
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
    }

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        registerMsg()
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        unRegisterMsg()
    }

    override fun onDrawForeground(canvas: Canvas?) {
        super.onDrawForeground(canvas)
        redDotController.onDrawForeground(canvas)

        if (debug) {
            if (debugPaint == null) {
                debugText = "${this.javaClass.simpleName}"
                debugPaint = initDebugPaint(context)
            }
            canvas?.save()
            if (name == null) {
                canvas?.drawText(
                    debugText!!,
                    0,
                    debugText!!.length,
                    measuredWidth / 2.0f,
                    measuredHeight / 2.0f,
                    debugPaint!!
                )
            } else {
                canvas?.drawText(
                    name!!,
                    0,
                    name!!.length,
                    measuredWidth / 2.0f,
                    measuredHeight / 2.0f,
                    debugPaint!!
                )
            }
            canvas?.restore()
        }
    }

    override fun removeRedDot() {
        redDotController.removeRedDor(this)
    }

    override fun showRedDot(msgTags: List<String>) {
        redDotController.showRedDor(msgTags, this)
    }

    override fun removeRedDot(msgTags: List<String>) {
        redDotController.removeRedDor(msgTags, this)
    }

    override var redDotTags: MutableList<String>
        get() = redDotController.tags!!
        set(value) {
            redDotController.tags = value
        }

    override fun isShowRedDot(): Boolean = redDotController.isShow()

    override fun redDotCircle(ox: Int, oY: Int) {
        redDotController.circle(ox, oY, this)
    }

    override fun registerMsg() {
        RedDotUtil.registerMsg(this)
    }

    override fun unRegisterMsg() {
        RedDotUtil.unRegisterMsg(this)
    }

    companion object {
        var debug = false

        fun debug(debug: Boolean) {
            this.debug = debug
        }

        private fun initDebugPaint(context: Context): Paint {
            return Paint().apply {
                this.color = Color.GREEN
                this.textAlign = Paint.Align.CENTER
                this.isAntiAlias = true
                this.textSize =
                    TypedValue.applyDimension(
                        TypedValue.COMPLEX_UNIT_SP,
                        18F,
                        context.resources.displayMetrics
                    )
                        .toFloat()
            }
        }
    }


    var name: String? = null

}